package com.hsteach.service.buildingApply;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.dao.domain.buildingApply.BuildingReviewPerson;
import com.hsteach.dao.mapper.buildingApply.BuildingReviewPersonMapper;
import com.hsteach.enums.ApplyTypeEnum;
import com.hsteach.enums.BuildingUseEnum;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetClassRoomListByPlaceIdRet;
import com.hsteach.service.third.XgkService;
import com.hsteach.web.controller.buildingApply.dto.ListBuildingReviewPersonDto;
import com.hsteach.web.controller.buildingApply.dto.UpdateBuildingReviewPersonDto;
import com.hsteach.web.controller.buildingApply.vo.ListBuildingReviewPersonVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 场馆申请审核人员配置Service业务层处理
 * 
 * @author fanxquer
 * @date 2023-08-27
 */
@Service
public class BuildingReviewPersonService
{
    @Autowired
    private BuildingReviewPersonMapper buildingReviewPersonMapper;

    @Autowired
    private XgkService xgkService;

    /**
     * 查询场馆申请审核人员配置
     * 
     * @param id 场馆申请审核人员配置主键
     * @return 场馆申请审核人员配置
     */
    public BuildingReviewPerson selectBuildingReviewPersonById(Long id)
    {
        return buildingReviewPersonMapper.selectBuildingReviewPersonById(id);
    }

    /**
     * 查询场馆申请审核人员配置列表
     * 
     * @param buildingReviewPerson 场馆申请审核人员配置
     * @return 场馆申请审核人员配置
     */
    public List<BuildingReviewPerson> selectBuildingReviewPersonList(BuildingReviewPerson buildingReviewPerson)
    {
        return buildingReviewPersonMapper.selectBuildingReviewPersonList(buildingReviewPerson);
    }

    /**
     * 查询场馆申请审核人员配置列表
     *
     * @param dto 场馆申请审核人员配置
     * @return 场馆申请审核人员配置
     */
    public List<ListBuildingReviewPersonVo> listBuildingReviewPerson(ListBuildingReviewPersonDto dto, ApplyTypeEnum type)
    {
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        BuildingReviewPerson query = new BuildingReviewPerson();
        query.setOrgCode(authInfo.getOrgCode());
        query.setApplyType(type.getCode());
        List<BuildingReviewPerson> buildingReviewPeople = buildingReviewPersonMapper.selectBuildingReviewPersonList(query);

        Map<String, BuildingReviewPerson> configMap = buildingReviewPeople.stream().collect(Collectors.toMap(e -> e.getBuildingUse(), e -> e, (v1, v2) -> v1));
        List<GetClassRoomListByPlaceIdRet> rooms = xgkService.getClassRoomListByPlaceId(0, authInfo.getOrgCode());
        List<String> useList = rooms.stream().map(GetClassRoomListByPlaceIdRet::getUseId).collect(Collectors.toList());
        // 过滤出当前学校需要审核的场所
        List<BuildingUseEnum> reviewRoom = BuildingUseEnum.getBuilding().stream().filter(e -> useList.contains(e.getCode())).collect(Collectors.toList());
        // 过滤出没有配置的场所，初始化配置为当前登录人
        List<BuildingUseEnum> noConfig = reviewRoom.stream().filter(e -> !configMap.containsKey(e.getCode())).collect(Collectors.toList());
        if (noConfig.size() > 0) {
            List<BuildingReviewPerson> inserts = noConfig.stream().map(e -> {
                BuildingReviewPerson person = new BuildingReviewPerson();
                person.setReviewPersonId(authInfo.getPersonId());
                person.setReviewPersonName(authInfo.getName());
                person.setBuildingUse(e.getCode());
                person.setOrgCode(authInfo.getOrgCode());
                person.setApplyType(type.getCode());
                return person;
            }).collect(Collectors.toList());
            buildingReviewPersonMapper.batchInsert(inserts);
            buildingReviewPeople = buildingReviewPersonMapper.selectBuildingReviewPersonList(query);
        }
        List<String> reviewRoomCode = reviewRoom.stream().map(BuildingUseEnum::getCode).collect(Collectors.toList());
        List<Long> deleteConfigId = configMap.entrySet().stream()
                .filter(e -> !reviewRoomCode.contains(e.getKey()))
                .map(Map.Entry::getValue)
                .map(BuildingReviewPerson::getId)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(deleteConfigId)) {
            buildingReviewPersonMapper.deleteBuildingReviewPersonByIds(deleteConfigId.toArray(new Long[deleteConfigId.size()]));
        }


        Map<String, BuildingReviewPerson> retConfigMap = buildingReviewPeople.stream().collect(Collectors.toMap(e -> e.getBuildingUse(), e -> e, (v1, v2) -> v1));
        // 主列表为基础配置已有的可申请场所
        List<ListBuildingReviewPersonVo> ret = reviewRoom.stream().map(e -> {
            BuildingReviewPerson buildingReviewPerson = retConfigMap.get(e.getCode());
            ListBuildingReviewPersonVo vo = new ListBuildingReviewPersonVo();
            BeanUtils.copyProperties(buildingReviewPerson, vo);
            vo.setBuildingUseText(e.getName());
            return vo;
        }).collect(Collectors.toList());

        return ret;
    }

    /**
     * 新增场馆申请审核人员配置
     * 
     * @param buildingReviewPerson 场馆申请审核人员配置
     * @return 结果
     */
    public int insertBuildingReviewPerson(BuildingReviewPerson buildingReviewPerson)
    {
        buildingReviewPerson.setCreateTime(DateUtils.getNowDate());
        return buildingReviewPersonMapper.insertBuildingReviewPerson(buildingReviewPerson);
    }

    public int updatePerson(UpdateBuildingReviewPersonDto dto, ApplyTypeEnum type)
    {
        BuildingReviewPerson query = new BuildingReviewPerson();
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        query.setApplyType(type.getCode());
        query.setBuildingUse(dto.getBuildingUse());
        List<BuildingReviewPerson> buildingReviewPeople = buildingReviewPersonMapper.selectBuildingReviewPersonList(query);
        if (buildingReviewPeople.size() == 0) {
            throw new ServiceException("未查找到数据");
        }
        if (buildingReviewPeople.size() > 1) {
            throw new ServiceException("数据有误，请联系管理员");
        }
        BuildingReviewPerson person = buildingReviewPeople.get(0);
        person.setReviewPersonId(dto.getReviewPersonId());
        person.setReviewPersonName(dto.getReviewPersonName());
        return buildingReviewPersonMapper.updateBuildingReviewPerson(person);
    }

    /**
     * 修改场馆申请审核人员配置
     * 
     * @param buildingReviewPerson 场馆申请审核人员配置
     * @return 结果
     */
    public int updateBuildingReviewPerson(BuildingReviewPerson buildingReviewPerson)
    {
        buildingReviewPerson.setUpdateTime(DateUtils.getNowDate());
        return buildingReviewPersonMapper.updateBuildingReviewPerson(buildingReviewPerson);
    }

    /**
     * 批量删除场馆申请审核人员配置
     * 
     * @param ids 需要删除的场馆申请审核人员配置主键
     * @return 结果
     */
    public int deleteBuildingReviewPersonByIds(Long[] ids)
    {
        return buildingReviewPersonMapper.deleteBuildingReviewPersonByIds(ids);
    }

    /**
     * 删除场馆申请审核人员配置信息
     * 
     * @param id 场馆申请审核人员配置主键
     * @return 结果
     */
    public int deleteBuildingReviewPersonById(Long id)
    {
        return buildingReviewPersonMapper.deleteBuildingReviewPersonById(id);
    }
}
